package edu.uwlax.cs.oayonlinestore.client.gui.shared;

import com.google.gwt.user.client.ui.CheckBox;

import edu.uwlax.cs.oayonlinestore.client.OnlinestoreRService;
import edu.uwlax.cs.oayonlinestore.client.utils.FormTable;
import edu.uwlax.cs.oayonlinestore.client.utils.DateEdit.InvalidDateException;
import edu.uwlax.cs.oayonlinestore.vo.OrderEditVO;

public class ShippingForm extends FormTable {

	private CreditCardEdit creditCard = new CreditCardEdit();

	private AddressEdit billAddr = new AddressEdit();

	private AddressEdit shipAddr = new AddressEdit();

	private CheckBox saveInProfile = new CheckBox();

	public ShippingForm(boolean allowEditStatus) {
		super();
		saveInProfile.setChecked(true);
		addSection("Payment Information");
		addInput(creditCard);
		addInput("Save CC No. in Profile", saveInProfile);
		addSection("Billing Address");
		addInput(billAddr);
		addSection("Shipping Address");
		addInput(shipAddr);
	}

	public void onButtonClicked() throws InvalidDateException {
		order.setCcCompany(creditCard.getInstitute().getSelection());
		order.setCcExpirationDate(creditCard.getExpDate().getDate());
		order.setCcNumber(creditCard.getNo().getText());

		order.setBillCity(billAddr.getCity().getText());
		order.setBillState(billAddr.getState().getText());
		order.setBillStreetAddr(billAddr.getStreet().getText());
		order.setBillZipCode(Integer.parseInt(billAddr.getZip().getText()));

		order.setShipCity(shipAddr.getCity().getText());
		order.setShipState(shipAddr.getState().getText());
		order.setShipStreetAddr(shipAddr.getStreet().getText());
		order.setShipZipCode(Integer.parseInt(shipAddr.getZip().getText()));

		OnlinestoreRService.Util.getInstance().updateOrderEdit(order,
				saveInProfile.isChecked(), getSaveHandler());
	}

	public void onDataLoad(Object data) {
		order = (OrderEditVO) data;
		creditCard.getNo().setText(order.getCcNumber());
		creditCard.getInstitute().setSelection(order.getCcCompany());
		creditCard.getExpDate().setDate(order.getCcExpirationDate());

		billAddr.getCity().setText(order.getBillCity());
		billAddr.getState().setText(order.getBillState());
		billAddr.getStreet().setText(order.getBillStreetAddr());
		billAddr.getZip().setText(order.getBillZipCode() + "");

		shipAddr.getCity().setText(order.getShipCity());
		shipAddr.getState().setText(order.getShipState());
		shipAddr.getStreet().setText(order.getShipStreetAddr());
		shipAddr.getZip().setText(order.getShipZipCode() + "");
	}

	private OrderEditVO order;

	public void loadData(int orderID) {
		OnlinestoreRService.Util.getInstance().getOrderEdit(orderID,
				getLoadHandler());
	}

}
